package com.dehong.controller;

import com.dehong.model.FactoryC;
import com.dehong.service.ContractCService;
import com.dehong.service.ContractProductCService;
import com.dehong.service.FactoryCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
public class FarctoryController {
    @Autowired
    private FactoryCService factoryCService;
    @Autowired
    private ContractProductCService contractProductCService;
    @RequestMapping("tologin")
    public String tologin(Model model){
        return "/pages/login";
    }

    @RequestMapping("selectfarctory")
    public String selectfarctory(Model model){
        List<FactoryC> list = factoryCService.selectfactory();
                model.addAttribute("dataList",list);
                return  "/pages/basicinfo/factory/jFactoryList";

    }
    @RequestMapping("goinsertfarctory")
    public String goinsertfarctory(Model model){
         return  "/pages/basicinfo/factory/jFactoryCreate";
    }
    @RequestMapping("insertfarctory")
    public String insertfarctory(Model model,FactoryC factoryC){
        factoryC.setCreateTime(new Date());
       factoryC.setState("1");
        factoryC.setFactoryId(UUID.randomUUID()+"");
        int insert = factoryCService.insert(factoryC);
       if (insert>0){
           return "redirect:/selectfarctory.action";
       }else {
           return  "error";
       }

    }
    @RequestMapping("factoryviews")
    public  String factoryview(Model model,FactoryC factoryC){
        FactoryC c = factoryCService.selectByPrimaryKey(factoryC.getFactoryId());
        model.addAttribute("obj",c);
        return "/pages/basicinfo/factory/jFactoryView";
    }
    @RequestMapping("goupdatefactroy")
    public  String  goupdatefactroy(Model model,String factoryId){
        FactoryC select = factoryCService.selectByPrimaryKey(factoryId);

        model.addAttribute("obj",select);
        return  "/pages/basicinfo/factory/jFactoryUpdate";

    }
    @RequestMapping("updatefactroy")
    public  String  goupdatefactroy(FactoryC factoryC){
        int i = factoryCService.updateByPrimaryKey(factoryC);
        if (i>0){
            return "redirect:/selectfarctory.action";
        }else {
            return "error";

        }
    }
    //批量删除
     @RequestMapping("moredelete")
    public String moredelete(String factoryId []){
         for (String s : factoryId) {
             contractProductCService.deleteByfactroyId(s);
         }
        int j =0;
         for (String s : factoryId) {
             int i = factoryCService.deleteByPrimaryKey(s);
             if (i>0){
                 j=j+i;
             }
         }
         if (j==factoryId.length){
             return "redirect:/selectfarctory.action";
         }else {
             return  "error";
         }

     }
     @RequestMapping("openuse")
     public  String  openuse(@RequestParam("factoryId") String factoryId){
         FactoryC factoryC = factoryCService.selectByPrimaryKey(factoryId);
         factoryC.setState("1");
         int i = factoryCService.updateByPrimaryKeySelective(factoryC);
         if (i>0){
             return  "redirect:/selectfarctory.action";
         }else {
             return  "error";
         }

     }
    @RequestMapping("closeuse")
     public String closeuse(@RequestParam("factoryId") String factoryId){
        FactoryC factoryC = factoryCService.selectByPrimaryKey(factoryId);
        factoryC.setState("2");
        int i = factoryCService.updateByPrimaryKeySelective(factoryC);
        if (i>0){
            return  "redirect:/selectfarctory.action";
        }else {
            return  "error";
        }
     }
}


